//
//  GameManager.swift
//  Game
//
//  Created by woniu on 2025/8/28.
//

import Foundation
import UIKit

class GameManager: NSObject {
    public static let share = GameManager()
    
    
    //删除图片
     func delete(imgNames:[String]){
         FileUtil.share.delete(imgNames: imgNames)
    }
    
    //保存图片
     func saveOrReplace(name:String,image:Data){
         FileUtil.share.saveOrReplace(name: name, image: image)
    }
   
    
    /// 获取指定类型数据，昨天的排在前面，今天的排在后面
    /// - Parameter type: 类型
    public func getAllGamesBy(type:GameType?)->[GameModel]{
        let contentList = FileUtil.share.getGameConfigList()
        var models:[GameModel] = []
        
        for gameDic in contentList{
            if let game = GameModel.deserialize(from: gameDic){
                //根据类型过滤
                if let filterType = type{
                    if game.type == filterType.rawValue{
                        for word in game.words{
                            word.image = FileUtil.share.getImageBy(word.imgName)
                        }
                        models.append(game)
                    }
                }else{
                    for word in game.words{
                        word.image = FileUtil.share.getImageBy(word.imgName)
                    }
                    models.append(game)
                }
            }
        }
        //昨天的排在前面，今天的排在后面
        let newList = models.sorted { item1, item2 in
            return item1.key < item2.key
        }
        
        return newList
    }
    
    
    /// 获取指定类型数据
    /// - Parameter type: 类型
    public func getAllGames_reversed(type:GameType?)->[GameModel]{
        
        let models = getAllGamesBy(type: type)
        let newList = Array(models.reversed())
        return newList
    }
    
    
    //写入文件
    public func saveGames(_ models:[GameModel]){
        for game in models{
            game.words = game.getSubmitWords()
        }
        let json = models.toJSONString() ?? ""
        print("保存Game:\(json)")
        if json.isEmpty{return}
        FileUtil.share.writeConfig(json)
    }
    
    //删除指定
    public func delete(gameKey:String){
        var imgNames:[String] = []
        
        var gameList = getAllGamesBy(type: nil)
        for (i,item) in gameList.enumerated(){
            if item.key == gameKey{
                for wordItem in item.words{
                    imgNames.append(wordItem.imgName)
                }
                gameList.remove(at: i)
                break
            }
        }
        saveGames(gameList)
        //删除图片文件
        GameManager.share.delete(imgNames: imgNames)
    }
    
    //更新或新增
    public func insertOrUpdate(game:GameModel){
        var gameList = getAllGamesBy(type: nil)
        var didFind = false
        for(i,oldGame) in gameList.enumerated(){
            if game.key == oldGame.key{
                gameList[i] = game
                didFind = true
                break
            }
        }
        if !didFind{
            gameList.insert(game, at: 0)
        }
        saveGames(gameList)
    }
    
    //获取图片
    public func getImageBy(_ name:String)->UIImage?{
        if name.replaceRN().isEmpty{return nil}
        return FileUtil.share.getImageBy(name)
    }
}
